/**
 * 	This file is part of Kayak.
 *
 *	Kayak is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Kayak is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public License
 *	along with Kayak.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.github.kayak.ui.messageview;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;

@ConvertAsProperties(dtd = "-//com.github.kayak.ui.messageview//Messages//EN",
autostore = false)
@TopComponent.Description(preferredID = "MessagesTopComponent",
iconBase="com/github/kayak/ui/icons/dialog-information.png",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@ActionID(category = "Window", id = "com.github.kayak.ui.messageview.MessagesTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.Registration(mode = "management", openAtStartup = true)
@TopComponent.OpenActionRegistration(displayName = "#CTL_MessagesAction",
preferredID = "MessagesTopComponent")
public final class MessagesTopComponent extends TopComponent implements ExplorerManager.Provider {

    private static final Logger logger = Logger.getLogger(MessagesTopComponent.class.getCanonicalName());

    private AbstractNode root;
    private ExplorerManager manager = new ExplorerManager();

    public MessagesTopComponent() {
	initComponents();
	setName(NbBundle.getMessage(MessagesTopComponent.class, "CTL_MessagesTopComponent"));
	setToolTipText(NbBundle.getMessage(MessagesTopComponent.class, "HINT_MessagesTopComponent"));

        BusFactory factory = new BusFactory();
	root = new AbstractNode(Children.create(factory, true));
	manager.setRootContext(root);
        associateLookup(ExplorerUtils.createLookup(manager, getActionMap()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                jLabel1 = new javax.swing.JLabel();
                jTextField2 = new javax.swing.JTextField();
                beanTreeView1 = new org.openide.explorer.view.BeanTreeView();

                org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(MessagesTopComponent.class, "MessagesTopComponent.jLabel1.text")); // NOI18N

                jTextField2.setText(org.openide.util.NbBundle.getMessage(MessagesTopComponent.class, "MessagesTopComponent.jTextField2.text")); // NOI18N
                jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
                        public void keyReleased(java.awt.event.KeyEvent evt) {
                                jTextField2KeyReleased(evt);
                        }
                });

                beanTreeView1.setRootVisible(false);

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
                this.setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                        .addComponent(beanTreeView1, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(beanTreeView1, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE))
                );
        }// </editor-fold>//GEN-END:initComponents

	private void jTextField2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField2KeyReleased
	    String filter = jTextField2.getText();
	    logger.log(Level.INFO, "filtering: {0}", filter);
	    if(filter.equals("")) {
		manager.setRootContext(root);
	    } else {
		SearchFilteredNode filteredNode = new SearchFilteredNode(root, filter);
	        manager.setRootContext(filteredNode);
                beanTreeView1.expandAll();
	    }
	}//GEN-LAST:event_jTextField2KeyReleased

        // Variables declaration - do not modify//GEN-BEGIN:variables
        private org.openide.explorer.view.BeanTreeView beanTreeView1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration//GEN-END:variables

    void writeProperties(java.util.Properties p) {
	// better to version settings since initial version as advocated at
	// http://wiki.apidesign.org/wiki/PropertyFiles
	p.setProperty("version", "1.0");
    }

    void readProperties(java.util.Properties p) {
	String version = p.getProperty("version");
    }

    @Override
    public ExplorerManager getExplorerManager() {
	return manager;
    }
}
